package tools;
import ChartDirector.*;
import lib.*;
import label.*;
import java.util.*;
import simuspec.*;
public class SpChart {
	static double startX=50;
	static double endX=0;

	static double viewPortStartX=0;
	static double viewPortEndX=0;


	public static void drawChart(WebChartViewer viewer)throws Exception
	{
		double totalDuration=endX-startX;
		double minDuration=viewer.getZoomInWidthLimit()*totalDuration;

		if(viewPortStartX<startX)
		{
			viewPortStartX=startX;
		}
		if(endX-viewPortStartX<minDuration)
		{
			viewPortStartX=endX-minDuration;
		}
		if(viewPortEndX>endX)
		{
			viewPortEndX=endX;
		}
		if(viewPortEndX-viewPortStartX<minDuration)
		{
			viewPortEndX=viewPortStartX=minDuration;
		}


		viewer.setViewPortWidth((viewPortEndX-viewPortStartX)/totalDuration);
		viewer.setViewPortLeft((viewPortStartX-startX)/totalDuration);
		
			//int rownum=0;
			
			String folder = "D:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/SpecSimulate/sun_test_data/";		
		    //java.io.File in = new java.io.File(folder);
		    //SearchSpLib.search(in);
		    
		    
		 //   double peak_group_raw[][]=SearchSpLib.dta_list.get(SearchSpLib.index_list.get(row.rownum-1));
		  //  double peak_group_raw[][]=SearchSpLib.dta_list.get(row.rownum-1);
		   // double p1[][]=SearchSpLib.exper_sp_resolve(new java.io.File(folder+"simu.cta"));
		      double p1[][]=SimuSpect.peakGroup;
		      
		      endX=p1[p1.length-1][0]+20;
		      startX=p1[0][0]-20;
		      //endX=1000;
		      
		      double[] x_data=ArrayManipulation.transpose(p1)[0];
		      double[] y_data=ArrayManipulation.transpose(p1)[1];
	        // The data for the bar chart
	         
			
		
			double peak_match[]=new double[x_data.length];
			
	        String peptide="GPAAIQK";
			
			XYChart c = new XYChart(800, 300);
			c.setPlotArea(50, 20, 730, 260);
	        MatchPeptide matchLabel=new MatchPeptide();
	        ArrayList<PeakLabel> label_list=matchLabel.getMatchInfor(x_data,peptide,0.5);
	        int colors_y[]=new int[x_data.length];
	        String labels_y[]=new String[x_data.length];
			for (int i = 0; i < x_data.length; i++) {
				PeakLabel peakCL = label_list.get(i);
				colors_y[i] = peakCL.color;
				labels_y[i] = peakCL.label;
			}
			BarLayer layer_bar_up=c.addBarLayer3(y_data,colors_y);
			layer_bar_up.setBarWidth(1);
			layer_bar_up.setXData(x_data);
			for (int i = 0; i < x_data.length; i++) {
				layer_bar_up.addCustomAggregateLabel(i, labels_y[i], "", 8, colors_y[i]); // set label's color!!!!!!!!!!!!!
			}
			layer_bar_up.setBorderColor(Chart.Transparent);
			

			/*
			 * the bottom sp peak init and set
			 */
			 /*
			BarLayer layer_bar_bottom = c.addBarLayer(y_data_2);
			layer_bar_bottom.setBarWidth(1);
			layer_bar_bottom.setBorderColor(0x00ff00);
			layer_bar_bottom.setXData(x_data);
			*/

			Chart.setLicenseCode("SXZVFNRN9MZ9L8LGA0E2B1BB");
			c.xAxis().setDateScale(viewPortStartX, viewPortEndX);
			
			if (viewer.getZoomDirection() == Chart.DirectionHorizontal) {
	        // y-axis is auto-scaled - so vertically, the view port always cover the entire y data
	        // range. We save the y-axis scale for supporting xy-zoom mode if needed in the future.
	        c.layout();
	        viewer.setCustomAttr("minValue", c.yAxis().getMinValue());
	        viewer.setCustomAttr("maxValue", c.yAxis().getMaxValue());
	        viewer.setViewPortTop(0);
	        viewer.setViewPortHeight(1);
	    } else {
	        // xy-zoom mode - retrieve the auto-scaled axis range, which contains the entire y data
	        // range.
	        double minValue = Double.parseDouble(viewer.getCustomAttr("minValue"));
	        double maxValue = Double.parseDouble(viewer.getCustomAttr("maxValue"));

	        // Compute the view port axis range
	        double axisLowerLimit = maxValue - (maxValue - minValue) * (viewer.getViewPortTop() +
	            viewer.getViewPortHeight());
	        double axisUpperLimit = maxValue - (maxValue - minValue) * viewer.getViewPortTop();

	        // Set the axis scale to the view port axis range
	        c.yAxis().setLinearScale(axisLowerLimit, axisUpperLimit);

	        // By default, ChartDirector will round the axis scale to the tick position. For zooming, we
	        // want to use the exact computed axis scale and so we disable rounding.
	        c.yAxis().setRounding(false, false);
	    }

	    String chartQuery = c.makeSession(viewer.getRequest(), viewer.getId());

	    String imageMap = c.getHTMLImageMap("", "",
	        "title='[{dataSetName}]:{value|2}'");

	   
	    viewer.setImageUrl("getchart.jsp?" + chartQuery);
	    viewer.setImageMap("getchart.jsp?" + viewer.makeDelayedMap(imageMap, true));
	    viewer.setChartMetrics(c.getChartMetrics());
			



	}
	public static void createFirstChart(WebChartViewer viewer)throws Exception
	{
		viewer.setMouseUsage(Chart.MouseUsageScroll);
		 //startX=0;
		 //endX=1600;

		 viewPortStartX=startX;
		 viewPortEndX=endX;

		 viewer.setCustomAttr("startX",startX);
		 viewer.setCustomAttr("endX",endX);

		 viewer.setZoomInWidthLimit(5 / (endX - startX));

		 drawChart(viewer);
	}

	public static void processPartialUpdate(WebChartViewer viewer)throws Exception
	{
		startX=Double.parseDouble(viewer.getCustomAttr("startX"));
		endX=Double.parseDouble(viewer.getCustomAttr("endX"));

		if(viewer.isViewPortChangedEvent())
		{
			double duration=endX-startX;
			viewPortStartX=startX+viewer.getViewPortLeft()*duration;
			viewPortEndX=viewPortStartX+viewer.getViewPortWidth()*duration;
		}
		else
		{

		}
		drawChart(viewer);



	}



}
